package com.isoft.illustrationsdraw.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.entity.ReportStatusLog;
import com.isoft.illustrationsdraw.service.AdminService;
import com.isoft.illustrationsdraw.service.JWTService;
import com.isoft.illustrationsdraw.service.ReportService;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    private final JWTService jwtService;
    private final AdminService adminService;
    private final ReportService reportService;

    public AdminController(ReportService reportService, JWTService jwtService, AdminService adminService) {
        this.jwtService = jwtService;
        this.adminService = adminService;
        this.reportService = reportService;
    }

    @GetMapping("/ban-someone")
    public ResponseEntity<?> banSomeone(@RequestParam String token, @RequestParam Long id) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (!jwtService.isAdmin(token)) {
            return ResponseEntity.badRequest().body("Not Admin!");
        }
        if (adminService.attemptBanSomeone(id)) {
            return ResponseEntity.ok("This guy has been banned");
        }
        return ResponseEntity.badRequest().body("Something went wrong.");
               
    }
    @GetMapping("/lock-illustration")
    public ResponseEntity<?> lockIllustration(@RequestParam String token, @RequestParam Long id) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (!jwtService.isAdmin(token)) {
            return ResponseEntity.badRequest().body("Not Admin!");
        }
        System.out.println(id);
        if (adminService.lockIllustration(id)) {
            return ResponseEntity.ok("This illustration has been banned");
        }
        return ResponseEntity.badRequest().body("Something went wrong.");
    } 

    @GetMapping("/delete-comment")
    public ResponseEntity<?> deleteComment(@RequestParam String token, @RequestParam Long id) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (!jwtService.isAdmin(token)) {
            return ResponseEntity.badRequest().body("Not Admin!");
        }
        if (adminService.forceToDeleteComment(id)) {
            return ResponseEntity.ok("This comment has been deleted forcely");
        }
        return ResponseEntity.badRequest().body("Something went wrong.");
    }

    @GetMapping("/get-report")
    public ResponseEntity<?> handleGetReport(@RequestParam String token, @RequestParam(required = false) String status) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (!jwtService.isAdmin(token)) {
            return ResponseEntity.badRequest().body("Not Admin!");
        }
        var result = reportService.getReports(status);

        if (result != null) {
            return ResponseEntity.ok(result);
        }
        return ResponseEntity.badRequest().body("Something went wrong.");
    }

    @PostMapping("/update-report")
    public ResponseEntity<?> handleUpdateReport(@RequestParam String token,@RequestBody String newLog) {
        if (!jwtService.validateToken(token)) {
            return ResponseEntity.badRequest().body(Constants.Error.INVALID_TOKEN);
        }
        if (!jwtService.isAdmin(token)) {
            return ResponseEntity.badRequest().body("Not Admin!");
        }
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            var newReportLog = objectMapper.readValue(newLog, ReportStatusLog.class);
            Long adminId = jwtService.getUserIdFromToken(token);
            newReportLog.setChangedBy(adminId);
            if (reportService.updateReportAndLog(newReportLog)) {

                return ResponseEntity.ok("Update successfully");
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        return ResponseEntity.badRequest().body("......");

    }

}
